package qiurank;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/12/31.
 */
public class ThreadManage extends Object{
    private List<Dothread> threadList;
    private boolean isFinish=false;
    private int MaxThreadCount=5; //开最大的线程个数
    private UrlManageInterface urlManage;
    public ThreadManage(){
        threadList=new ArrayList<Dothread>();

        try {
            urlManage=(UrlManageInterface)Class.forName("app."+Config.getValue("package")+".UrlManage").getConstructors()[0].newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //urlManage.addUrl(Config.BASE_URL,11);
    }
    public void createNewThread(){
        if(MaxThreadCount>threadList.size()) {
            Dothread dothread = new Dothread(urlManage,this);
            dothread.setDaemon(true);
            threadList.add(dothread);
            dothread.setIndex(threadList.size());
            dothread.start();

        }else {
            Log.show("最大线程数超出,不能再创建线程了,最大线程数:"+MaxThreadCount+",当前线程数:"+getThreadCount());
        }
    }
    //重新开始线程
    public void restartAll(){
        for(Dothread dothread:threadList){
            dothread.restart();
        }
    }
    public void setFinish(){
        isFinish=true;
    }
    public boolean getFinish(){
        return isFinish;
    }
    /**
     * 暂停所有线程
     */
    public void pauseAllThread(){
        for(Dothread dothread:threadList){
            dothread.pause();
        }
    }
    /*
    结束所有线程
     */
    public void finishAllThread(){
        for(Dothread dothread:threadList){
            dothread.finish();
        }
    }
    /**
     * 线程的个数
     * @return 线程的个数
     */
    public int getThreadCount(){
        return threadList.size();
    }

    /**
     * 设置最大的线程个数
     * @param maxThreadCount 最大线程个数
     */
    public void setMaxThreadCount(int maxThreadCount){
        MaxThreadCount=maxThreadCount;
    }
}
